package com.charles.seckillPlatform.controller;

import com.charles.seckillData.domain.SysAddr;
import com.charles.seckillComponentCommon.controller.BaseController;
import com.charles.seckillComponentCommon.domain.AjaxResult;
import com.charles.seckillComponentCommon.domain.TableInfo;
import com.charles.seckillComponentSecurity.utils.ThreadUtil;
import com.charles.seckillPlatform.mapper.SysAddrMapper;
import com.charles.seckillPlatform.service.SysAddrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/addr")
public class SysAddrController extends BaseController {

    @Autowired
    private SysAddrMapper sysAddrMapper;

    @Autowired
    private SysAddrService sysAddrService;

    @GetMapping("/queryAddressTableList")
    public TableInfo queryAddressTableList() {
        startPage();
        return getDataTable(sysAddrMapper.queryAddrTableList(Long.parseLong(ThreadUtil.getUserId())));
    }

    @PostMapping("/createAddress")
    public AjaxResult createAddress(@RequestBody SysAddr sysAddr) {
        sysAddrService.checkAddrForm(sysAddr);
        sysAddr.setUser_id(Long.parseLong(ThreadUtil.getUserId()));
        return toAjax(sysAddrMapper.createAddress(sysAddr));
    }

    @GetMapping("/queryModifyAddrForm/{addr_id}")
    public AjaxResult queryModifyAddrForm(@PathVariable("addr_id") Long addr_id) {
        return AjaxResult.success(sysAddrMapper.queryAddrById(addr_id));
    }

    @PutMapping("/modifyAddress")
    public AjaxResult modifyAddress(@RequestBody SysAddr sysAddr) {
        sysAddrService.checkAddrForm(sysAddr);
        return toAjax(sysAddrMapper.modifyAddress(sysAddr));
    }

    @DeleteMapping("/deleteAddr/{addr_id}")
    public AjaxResult deleteAddr(@PathVariable("addr_id") Long addr_id) {
        return toAjax(sysAddrMapper.deleteAddress(addr_id));
    }

}
